package cn.sfa.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sfa.common.taglib.CommonCache;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.ConfigUtil;
import cn.sfa.entity.system.BranchSysCode;
import cn.sfa.entity.system.ProviderSysCode;
import cn.sfa.service.routine.ExChangeCalendarService;

@Controller
@RequestMapping("/common/index")
public class CommonController {

	private Logger log = LoggerFactory.getLogger(CommonController.class);



	@RequestMapping("/Shortcut1")
	public void Shortcut1(HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out;
		String shortcutUrl;
		String shortcutIconFileUrl;
		String shortcutIconName;
		try {
			Configuration config = ConfigUtil
					.getXmlConfig(ExChangeCalendarService.class
							.getClassLoader().getResource("/sys_config.xml")
							.getPath());
			shortcutUrl = config.getString("shortcut-url");
			shortcutIconFileUrl = config.getString("shortcut-iconfile");
			shortcutIconName = config.getString("shortcut-iconname");
		} catch (ConfigurationException e) {
			throw new RuntimeException(e + "配置文件初始化错误");
		}
		
		FileSystemView fsv = FileSystemView.getFileSystemView();
		File f = fsv.getHomeDirectory();
		String path = f.getPath();
		String templateContent = "[InternetShortcut]" + "\n" + "URL="
				+ shortcutUrl + "\n" + "Modified=F00F43B3A875C601D1" + "\n"
				+ "IconIndex=0" + "\n" + "IconFile=" + shortcutIconFileUrl
				+ "\n" + "Prop3=19,2";
		String realfilename = shortcutIconName + ".url";
		String upurl = path;
		String filename = upurl + "/" + realfilename;
		File myfile = new File(filename);

		try {
			if (!myfile.exists()) {
				FileOutputStream fileoutputstream = new FileOutputStream(
						filename);// 建立文件输出流
				byte tag_bytes[] = templateContent.getBytes();
				fileoutputstream.write(tag_bytes);
				fileoutputstream.close();
			}

			try {
				File file = new File(upurl, realfilename);
				BufferedInputStream bis = new BufferedInputStream(
						new FileInputStream(file));
				byte[] buffer = new byte[111000];
				realfilename = java.net.URLEncoder
						.encode(realfilename, "UTF-8");
				response.reset();
				response.setCharacterEncoding("UTF-8");
				response.setContentType("application/x-download");// 不同类型的文件对应不同的MIME类型
				response.setHeader("Content-Disposition",
						"attachment; filename=" + realfilename);
				OutputStream os = response.getOutputStream();
				while (bis.read(buffer) > 0) {
					os.write(buffer);
				}
				bis.close();
				os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}

			response.setContentType("text/html;charset=UTF-8");
			out = response.getWriter();
			String result = filename;
			out.print(result);
			out.flush();
			out.close();
		} catch (Exception e) {
			try {
				response.setContentType("text/html;charset=UTF-8");
				out = response.getWriter();
				String result = "NO";
				out.print(result);
				out.flush();
				out.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}

		}
	}

	@RequestMapping("/Shortcut")
	public void Shortcut(HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out;
		String shortcutUrl;
		String shortcutIconFileUrl;
		String shortcutIconName;
		String upurl;
		try {
			Configuration config = ConfigUtil
					.getXmlConfig(ExChangeCalendarService.class
							.getClassLoader().getResource("/sys_config.xml")
							.getPath());
			shortcutUrl = config.getString("shortcut-url");
			shortcutIconFileUrl = config.getString("shortcut-iconfile");
			shortcutIconName = config.getString("shortcut-iconname");
			upurl = config.getString("upload-url");

		} catch (ConfigurationException e) {
			throw new RuntimeException(e + "配置文件初始化错误");
		}
		String templateContent = "[InternetShortcut]" + "\n" + "URL="
				+ shortcutUrl + "\n" + "Modified=F00F43B3A875C601D1" + "\n"
				+ "IconIndex=0" + "\n" + "IconFile=" + shortcutIconFileUrl
				+ "\n" + "Prop3=19,2";
		String realfilename = shortcutIconName + ".url";
		String filename = upurl + realfilename;
		filename = filename.replace("\\", "/");
		File myfile = new File(filename);

		try {
			if (!myfile.exists()) {
				FileOutputStream fileoutputstream = new FileOutputStream(
						filename);// 建立文件输出流
				byte tag_bytes[] = templateContent.getBytes();
				fileoutputstream.write(tag_bytes);
				fileoutputstream.close();
			}
			try {
				File file = new File(upurl, realfilename);
				BufferedInputStream bis = new BufferedInputStream(
						new FileInputStream(file));
				byte[] buffer = new byte[111000];
				realfilename = java.net.URLEncoder
						.encode(realfilename, "UTF-8");
				response.reset();
				response.setCharacterEncoding("UTF-8");

				response.setContentType("application/octet-stream");
				response.setHeader("Content-Disposition","attachment; filename=" + realfilename);
				OutputStream os = response.getOutputStream();
				while (bis.read(buffer) > 0) {
					os.write(buffer);
				}
				bis.close();
				os.close();
				return;
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
